package com.example.anew.journey.mynewjourney.logic.getVersion;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.BaseRegisteredAndLoginActivity;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.FirstChangeMainActivity;
import com.example.anew.journey.mynewjourney.logic.getVersion.persenter.GetVersionPersenter;
import com.example.anew.journey.mynewjourney.logic.getVersion.view.ISGetVersionView;
import com.example.anew.journey.mynewjourney.logic.setName.SetNameActivity;

import cn.jesse.nativelogger.NLogger;

/**
 * 杭州惟君医疗科技有限公司11
 * Created by 20433 on 2018/3/5
 * @desc 获取验证码
 */

public class GetVersionActivity extends BaseRegisteredAndLoginActivity implements View.OnClickListener,ISGetVersionView {

    private TextView tv_dec;
    private TextView tv_prompt;
    private TextView tv_send_code;
    private EditText et_content_version_code;
    private Button bt_next;
    private String phone;
    private GetVersionPersenter mGetVersionPersenter = new GetVersionPersenter(this);
    private int my_login_state;


    public String third_party_login_id;
    public String third_party_login_name;
    public String third_party_login_state;

    /**
     * @desc 初始化数据 onCreate()
     */
    @Override
    protected void initView(View view) {
        // todo Toolbar的一系列操作
        setToolbarBg(0xffffffff);
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        my_login_state = intent.getIntExtra("my_login_state",0);
        NLogger.e("TAG","=====4===NetworkResponseDate=====45======="+my_login_state);

        third_party_login_id = intent.getStringExtra("third_party_login_id");
        third_party_login_name = intent.getStringExtra("third_party_login_name");
        third_party_login_state = intent.getStringExtra("third_party_login_state");
        NLogger.e("TAG","=========state=====1008===="+third_party_login_id);
        NLogger.e("TAG","=========state=====1009===="+third_party_login_name);
        NLogger.e("TAG","=========state=====10010===="+third_party_login_state);

        changTimeOne();
        findGetVersionView(view);
       // mGetVersionPersenter.getPasswordFreeLoginVersionCode("13372509189");
        //mGetVersionPersenter.requestNetworkGetVersionCode(this);
    }

    /**
     * @desc 主要内容的布局资源填充进来
     */
    @Override
    protected View setMainContentView() {
        return View.inflate(this, R.layout.activity_get_version_code, null);
    }
    /**==================方法区====================
     * @param view*/
    private void findGetVersionView(View view) {
        tv_dec = (TextView) findViewById(R.id.activity_get_version_code_tv_dec);
        tv_prompt = (TextView) findViewById(R.id.activity_get_version_code_tv_prompt);
        tv_send_code = (TextView) findViewById(R.id.activity_get_version_code_tv_send_code);
        et_content_version_code = (EditText) findViewById(R.id.activity_get_version_code_et_input_version_code);
        bt_next = (Button) findViewById(R.id.activity_get_version_code_bt_next);

        tv_send_code.setOnClickListener(this);
        bt_next.setOnClickListener(this);


        String prompt = setTvPromPt();
        if(TextUtils.isEmpty(prompt)) return;
        tv_prompt.setText(prompt);
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.activity_get_version_code_tv_send_code:
                if (util.check()) return;
                mGetVersionPersenter.requestNetworkGetVersionCode(this);
                break;
            case R.id.activity_get_version_code_bt_next:
                if (util.check()) return;
                mGetVersionPersenter.OnClickNext(GetVersionActivity.this);
                break;
        }
    }
    // 提示信息
    private String setTvPromPt() {
        if(TextUtils.isEmpty(phone)) return "";
        return "验证码已发送至 "+phone;
    }
    // 获取提示信息
    @Override
    public String getTvPrompt() {
        return null;
    }
    // 设置提示信息
    @Override
    public void setTvPrompt(String str) {

    }
    // 获取输入验证码
    @Override
    public String getEtInputVersionCode() {
        return et_content_version_code.getText().toString().trim();
    }
    // 设置验证码
    @Override
    public void setEtInputVersionCode(String str) {

    }
    // 获取时间
    @Override
    public String getTvSendCode() {
        return null;
    }
    // 设置时间
    @Override
    public void setTvSendCode(String str) {
        tv_send_code.setText(str);
    }
    // 跳转 设置名字页面
    @Override
    public void toSetNameActivity(String trim) {
        Intent intent =  new Intent(GetVersionActivity.this,SetNameActivity.class);
        intent.putExtra("phone",phone);
        intent.putExtra("version_code",trim);
        if(getThirdPartyLoginState() != null && getThirdPartyLoginState().equals("1")) {
            intent.putExtra("third_party_login_state",third_party_login_state);  //Third party login
            intent.putExtra("third_party_login_id",third_party_login_id);  //Third party login
            intent.putExtra("third_party_login_name",third_party_login_name);
            NLogger.e("TAG","=========state=====1011===="+third_party_login_id);
            NLogger.e("TAG","=========state=====1012===="+third_party_login_name);
            NLogger.e("TAG","=========state=====10013===="+third_party_login_state);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        startActivity(intent);

    }

    @Override
    public void showToastInformation(String str) {
       // NLogger.e("tag","submitRegisteredOrLogin======11===toFirstChangeMainPager==");
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getPhoneNumber() {
        return phone;
    }
    //获取状态
    @Override
    public int getMyState() {
        return my_login_state;
    }
    //退回前一个页面
    @Override
    public void toFinshBeforePager() {
        //TODO 定时退出


    }
    //跳转到主页
    @Override
    public void toFirstChangeMainPager() {
        Intent intent =  new Intent(GetVersionActivity.this,FirstChangeMainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    public void changTimeOne() {
        if(flag) changTime();
    }

    @Override
    public void findshpager() {
        finish();
    }

    @Override
    public String getThirdPartyLoginId() {
        return third_party_login_id;
    }

    @Override
    public String getThirdPartyLoginName() {
        return third_party_login_name;
    }

    @Override
    public String getThirdPartyLoginState() {
        return third_party_login_state;
    }

    /**
     * @desc Activity 销毁 释放资源
     */
    @Override
    protected void releaseResources() {

    }
    private Handler mHandler   = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.arg1 == 0) {
                flag = true;
                setTvSendCode("获取验证码");
            } else {
                flag = false;
                setTvSendCode(msg.arg1+" S" );
            }
        }
    };
    private boolean flag = true;

    private void  changTime(){
        flag = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 59; i >= 0 ; i--) {
                    try {
                        Message msg = mHandler.obtainMessage();
                        msg.arg1 = i;
                        mHandler.sendMessage(msg);
                        Thread.sleep(1000);
                    } catch (Exception e) {
                    }
                }
            }
        }).start();
    }



    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        NLogger.e("TAG","=========222=fgcj===========");
    }


}
